//
//  DJSearchViewController.m
//  Adorable_Things
//
//  Created by lanou on 16/1/16.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "DJSearchViewController.h"
#import "DJCommentListModel.h"
#import "DJAttentionCollectionViewCell.h"
#import "ATMainDetailViewController.h"

#define kSearchUrl @"http://api.menghuoapp.com/v1/item/search"

@interface DJSearchViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) NSMutableArray *listArr;
@property (nonatomic, strong) UICollectionView *collectionV;
@property (nonatomic, strong) UITextField *searchTF;

@end

@implementation DJSearchViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(10, 25, 20, 25);
    [backButton setImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:backButton];
    
    self.searchTF = [[UITextField alloc]initWithFrame:CGRectMake((kWidth - 200) / 2, 20, 200, 30)];
    self.searchTF.layer.cornerRadius = 15;
    self.searchTF.layer.masksToBounds = YES;
    self.searchTF.placeholder = @" 搜索本店商品";
    self.searchTF.text = self.name;
    self.searchTF.delegate = self;
    self.searchTF.backgroundColor = [UIColor lightGrayColor];
    self.searchTF.textColor = [UIColor blackColor];
    self.searchTF.keyboardType = UIKeyboardAppearanceDefault;
    self.searchTF.returnKeyType = UIReturnKeySearch;
    self.searchTF.clearButtonMode = UITextFieldViewModeAlways;
    self.searchTF.alpha = 0.5;
    self.searchTF.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:self.searchTF];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(kWidth - 40, 25, 20, 20);
    [button setTitleColor:[UIColor cyanColor] forState:(UIControlStateNormal)];
    [button setImage:[UIImage imageNamed:@"iconfont-sousuo"] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(searchAction:) forControlEvents:(UIControlEventTouchUpInside)];
    button.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:button];
    
    [self setupdata];
    [self addsubView];
}

- (void)setupdata
{
    // order_by=1&order_desc=1&page=0&page_size=20&q=&shop_id=187
    //order_by=1&order_desc=1&page=0&page_size=20&q=%E8%87%AA%E5%88%B6&shop_id=187
    

    NSDictionary *pardic = nil;
    if (self.name.length == 0) {
        pardic = @{@"order_by":@"1",@"order_desc":@"1",@"page":@"0",@"page_size":@"20",@"q":[self.searchTF.text stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],@"shop_id":self.shop_id};

    }else{
        pardic = @{@"order_by":@"1",@"order_desc":@"1",@"page":@"0",@"page_size":@"20",@"q":[self.searchTF.text stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]};
    
    }
    
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:kSearchUrl parameters:pardic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.listArr = [NSMutableArray array];
        NSArray *dataArr = responseObject[@"data"];
        for (NSDictionary *oneDic in dataArr) {
            DJCommentListModel *model = [[DJCommentListModel alloc]init];
            [model setValuesForKeysWithDictionary:oneDic];
            [self.listArr addObject:model];
        }
        [self.collectionV reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

- (void)addsubView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.itemSize = CGSizeMake( (kWidth - 30 ) / 2, 250);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 50, kWidth, self.view.height - 50) collectionViewLayout:layout];
    self.collectionV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionV];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    
    [self.collectionV registerNib:[UINib nibWithNibName:@"DJAttentionCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"AttentionCollectionCell"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.listArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DJAttentionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AttentionCollectionCell" forIndexPath:indexPath];
    DJCommentListModel *model = self.listArr[indexPath.row];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.pic_url]];
    cell.imageV.layer.cornerRadius = 5;
    cell.imageV.layer.masksToBounds = YES;
    cell.nameLab.text = model.title;
    cell.priceLab.text = [NSString stringWithFormat:@"￥%@", model.price];
    cell.Lab.text = [NSString stringWithFormat:@"人气%@",model.favorite_num];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // cell点击方法
    DJCommentListModel *model = self.listArr[indexPath.row];
    ATMainDetailViewController *mainVC = [[ATMainDetailViewController alloc]init];
    mainVC.item_id = model.item_id;
    [self.navigationController pushViewController:mainVC animated:YES];
}

- (void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchAction:(UIButton *)button
{
    [self setupdata];
    [self.searchTF resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self setupdata];
    [self.searchTF resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
